package api_test

import (
	"context"
	"gitee.com/kristas/booting-go/framework"
	file2 "gitee.com/kristas/booting-go/framework/common/util/file"
	"gitee.com/kristas/booting-go/framework/core/statement/types"
	"gitee.com/kristas/booting-go/framework/web/rest"
	model2 "gitee.com/kristas/booting-go/internal/demo/model"
	"mime/multipart"
	"net/http"
)

func init() {
	framework.Component(new(TestInjectExtraController))
}

type TestInjectExtraController struct {
	types.Controller `group:"/test/inject/extra"`
	testForm         rest.POST `url:"/form" param:"[Int,Float,Bool,String,Map form]"`
	testMultipart    rest.POST `url:"/multipart" param:"[file,file,file multipart]"`
	testContext      rest.GET  `url:"/context" param:"[c context]"`
	testReqNResp     rest.GET  `url:"/request_response" param:"[r request,w response]"`
	testStream       rest.POST `url:"/stream" param:"[s stream]"`
}

func (r *TestInjectExtraController) TestPath(i int, f float64, bl bool, s string) interface{} {
	return map[string]interface{}{
		"source": "path",
		"int":    i,
		"float":  f,
		"bool":   bl,
		"string": s,
	}
}

func (r *TestInjectExtraController) TestQuery(i int, f float64, bl bool, s string, account model2.Account) interface{} {
	return map[string]interface{}{
		"source": "query",
		"int":    i,
		"float":  f,
		"bool":   bl,
		"string": s,
		"struct": account,
	}
}

func (r *TestInjectExtraController) TestBody(account model2.Account) interface{} {
	return map[string]interface{}{
		"source": "body",
		"struct": account,
	}
}

func (r *TestInjectExtraController) TestForm(i int, f float64, bl bool, s string, m map[string][]string) interface{} {
	return map[string]interface{}{
		"source": "form",
		"int":    i,
		"float":  f,
		"bool":   bl,
		"string": s,
		"map":    m,
	}
}

func (r *TestInjectExtraController) TestMultipart(file multipart.File, header *multipart.FileHeader, value string) interface{} {
	return map[string]interface{}{
		"file":     value,
		"size":     file2.FormatFileSize(header.Size),
		"fileName": header.Filename,
	}
}

func (r *TestInjectExtraController) TestHeader(token string) string {
	return "token is " + token
}

func (r *TestInjectExtraController) TestContext(c context.Context) string {
	time, _ := c.Deadline()
	return time.Format(http.TimeFormat)
}

func (r *TestInjectExtraController) TestReqNResp(req *http.Request, w http.ResponseWriter) {
	w.Write([]byte(req.Host + req.RequestURI))
}

func (r *TestInjectExtraController) TestStream(s []byte) interface{} {
	return string(s)
}
